package com.zxw.hotel.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.dao.ReserveDao;
import com.zxw.hotel.model.Reserve;
import com.zxw.hotel.model.extend.ReserveExtend;
import com.zxw.hotel.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReserveServiceImpl implements ReserveService {
    private ReserveDao reserveDao;
    @Autowired
    public void setReserveDao(ReserveDao reserveDao) {
        this.reserveDao = reserveDao;
    }

    @Override
    public List<Reserve> getAll(Pageinfo pageinfo, ReserveExtend reserveExtend) {
        if(reserveExtend.getRoomId()!=null){
            reserveExtend.setRoomId(reserveExtend.getRoomId());
        }
        try(Page<?> __= PageHelper.startPage(pageinfo.getPageNo(), pageinfo.getPageSize())){
        return reserveDao.getAll( reserveExtend);
    }

    }

    @Override
    public int delete(Integer[] ids) {
        return reserveDao.delete(ids);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int update(ReserveExtend reserveExtend) {
        if(reserveExtend.getUserId()==null||reserveExtend.getUserId().describeConstable().isEmpty()||
                reserveExtend.getRoomId()==null||reserveExtend.getRoomId().describeConstable().isEmpty()||
                reserveExtend.getBirthFrom()==null||reserveExtend.getBirthTo()==null){
            return 0;
        }
       reserveExtend.setBirthFrom(reserveExtend.getBirthFrom().plusDays(1));
       reserveExtend.setBirthTo(reserveExtend.getBirthTo().plusDays(1));
        //根据roomid从reserve表中取出开始时间和结束时间
        String selectbyuserid = reserveDao.selectbyuserid(reserveExtend.getUserId());
        if(selectbyuserid==null){
            return 0;
        }
        //查询roomid是否存在
        String selectbyroomid = reserveDao.selectbyreserveid(reserveExtend.getRoomId());
        if(selectbyroomid==null){
            return 0;
        }
        Integer[] id= new Integer[1];
        id[0]=reserveExtend.getId();
        reserveDao.delete(id);
        List<ReserveExtend> selectbyid = reserveDao.selectbyid(reserveExtend.getRoomId());
       //更新

        if(!selectbyid.isEmpty()){
            for (ReserveExtend reserveExtend1 : selectbyid) {
                if(reserveExtend1.getBirthFrom().isBefore(reserveExtend.getBirthTo())&&
                        reserveExtend1.getBirthTo().isAfter(reserveExtend.getBirthFrom())){
                    return 0;
                }
            }
            try{
                int insert = reserveDao.insert(reserveExtend);
                return insert;
            }catch (Exception e){
                return 0;
            }

        }
        try{
            int insert = reserveDao.insert(reserveExtend);
            return insert;
        }catch (Exception e){
            return 0;
        }
    }




    @Override
    public int insert(ReserveExtend reserveExtend) {
        if(reserveExtend.getUserId()==null||reserveExtend.getUserId().describeConstable().isEmpty()||
        reserveExtend.getRoomId()==null||reserveExtend.getRoomId().describeConstable().isEmpty()||
                reserveExtend.getBirthFrom()==null||reserveExtend.getBirthTo()==null){
return 0;
        }
        reserveExtend.setBirthFrom(reserveExtend.getBirthFrom().plusDays(1));
        reserveExtend.setBirthTo(reserveExtend.getBirthTo().plusDays(1));
        //userid roomid 开始时间 结束时间 备注
        //根据roomid从reserve表中取出开始时间和结束时间
        //查询userid是否对应username
        String selectbyuserid = reserveDao.selectbyuserid(reserveExtend.getUserId());
        if(selectbyuserid==null){
            return 0;
        }
        //查询roomid是否存在
        String selectbyroomid = reserveDao.selectbyreserveid(reserveExtend.getRoomId());
        if(selectbyroomid==null){
            return 0;
        }
        List<ReserveExtend> selectbyid = reserveDao.selectbyid(reserveExtend.getRoomId());
        if(!selectbyid.isEmpty()){
            for (ReserveExtend reserveExtend1 : selectbyid) {
                if(reserveExtend1.getBirthFrom().isBefore(reserveExtend.getBirthTo())&&
                        reserveExtend1.getBirthTo().isAfter(reserveExtend.getBirthFrom())){
                    return 0;
                }
            }
            try{
                int insert = reserveDao.insert(reserveExtend);
                return insert;
            }catch (Exception e){
                return 0;
            }
        }
        try{
            int insert = reserveDao.insert(reserveExtend);
            return insert;
        }catch (Exception e){
            return 0;
        }



    }
}
